import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
import '../models/user_model.dart';

class UserLogService {
  static const String _logKey = 'user_logs';

  // 添加一条日志
  static Future<void> addLog(UserLog log) async {
    final prefs = await SharedPreferences.getInstance();
    final logsJson = prefs.getStringList(_logKey) ?? [];
    logsJson.add(jsonEncode(log.toJson()));
    await prefs.setStringList(_logKey, logsJson);
  }

  // 获取所有日志
  static Future<List<UserLog>> getLogs() async {
    final prefs = await SharedPreferences.getInstance();
    final logsJson = prefs.getStringList(_logKey) ?? [];
    return logsJson.map((e) => UserLog.fromJson(jsonDecode(e))).toList();
  }

  // 清空日志
  static Future<void> clearLogs() async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.remove(_logKey);
  }
} 